<?php
namespace App\Http\Service\api;

use Illuminate\Support\Facades\DB;
use Yansongda\Pay\Log;
use Yansongda\Pay\Pay;

class AliPay
{
    /**
     * @var array
     * 配置文件
     */
    protected $config = [
        'app_id' => '2021000119635301',
        'return_url' => 'http://991129.fun/api/pay/aliPaySuccess',
        'ali_public_key' =>'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsCTvFSqu6VXYswqpnw2TdYylgYmMmX91v49x8RSA+mW1yoR/ZwPaVTIeNW5nIhokbKf/jlWzxacY+axglZ/ASet+LzMGBkSn/lrCf8k9g1vybMBMJtU1Hz0t/dwvs6180DpiOz0cl1LrRl+vAMUaEDDgj3ZDzSF9WEUV2PJqSDvXcQ5ykSWrZ1DIrOYtlcJ9BYrQqvg1wXHR8UuEHOMuTMNQxP4Gmwo1x4dDeDtaC60Lkc4klNenYvnCb9D2CoWmcDlZAH4ER5RK3cauDdYRAKAwwyf9tMb3IFlJrwwT9z/P7mZ35wOhWzIb9Nb3HlJGKAsb/bsatCFumINXVH+YCwIDAQAB',         // 加密方式： **RSA2**
        'private_key' =>'MIIEowIBAAKCAQEAhxODr0hGbrIUkGJ++E4uBPv6q0GE1cJNKBe++yLFuKG3NGkseqAe4BwdgpdvIGUDFJ2dfKSh0tq1OK8uq19UDjk3TjukYcM6h87tLuMaPCmDrte3WjDcnICzJBBg9HSIc6ONMEs/Xagin4jPhyNMs0+LD4nDWKM9giFk8vDqlA9N1sqvxu4BldAmLvwemZo/k4UQKz3nzWYeam+DnOZbKhH+FyyxrekLrQ8sK8xFO8ylDnFDrPWwAzUwwNlmNjynkC0XbKGM/PJDjGhnOK8q4OjQAPDSh9/W3cAdjoO1RUw7ko3LmoU+fJtqXZeZgYUJdzKplnM2fhHdzD0wL2pkQwIDAQABAoIBAAic+vGrZypZ2qvWTAhjW9Xfh31yIRlMWYUr9nanIfqC/V6vMjHjkfRydoNESsbDCi4/WrJ/AP6kCZhCu3QcEQ0TIu6vKO0++LV+QWu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HDe8RS1dH01bKbpeYSuoWxQHj2g1D7RtW5',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
    ];

    /**
     * @param $data
     * @return \Symfony\Component\HttpFoundation\Response
     * 支付
     */
    public function payIndex($data){
        $order = [
            'out_trade_no' => $data['order_number'],//订单号
            'total_amount' => $data['order_money'],  //金额
            'subject' => $data['order_name'],//名称
        ];

        $alipay = Pay::alipay($this->config)->web($order);

        return $alipay;// laravel 框架中请直接 `return $alipay`
    }

    /**
     * @return \Yansongda\Supports\Collection
     * @throws \Yansongda\Pay\Exceptions\InvalidConfigException
     * @throws \Yansongda\Pay\Exceptions\InvalidSignException
     * 支付成功回调参数
     */
    public function paySuccess(){
        return Pay::alipay($this->config)->verify(); // 是的，验签就这么简单！
    }

    /**
     * @param $data
     * 提现及转账
     */
    public function payDpost($data){
        $order = [
            'out_biz_no' => $data['order_des'], //商户转账唯一订单号
            'payee_type' => 'ALIPAY_LOGONID',
            'payee_account' => $data['user_account'],
            'amount' => $data['money_des'],
            'remark' => '提现余额'
        ];
        $alipay = Pay::alipay($this->config);
        $result = $alipay->transfer($order);
        return $result;
    }

    /**
     * @param $data
     * @return \Yansongda\Supports\Collection
     * @throws \Yansongda\Pay\Exceptions\GatewayException
     * @throws \Yansongda\Pay\Exceptions\InvalidConfigException
     * @throws \Yansongda\Pay\Exceptions\InvalidSignException
     * 退款
     */
    public function refundPay($data){
        $order = [
            'out_trade_no' =>  $data['order_id'],
            'refund_amount' =>  $data['order_number'],
        ];
        $alipay = Pay::alipay($this->config);
        $result = $alipay->refund($order);
        return $result;
    }
}
